package cn.dujc.superkid.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;

import java.io.File;
import java.io.FileInputStream;

import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;

/**
 * 图片加载中间层
 * Created by du on 2017/9/26.
 */
public class ImageLoadHelper {
    private ImageLoadHelper() {
    }

    public static void loadAvatar(ImageView imageView, String url) {
        loadImage(null, imageView, url, R.mipmap.selection_holder);
    }

    public static void loadAvatar(Context context, ImageView imageView, String url) {
        loadImage(context, imageView, url, R.mipmap.selection_holder);
    }

    public static void loadImage(ImageView imageView, String url) {
        loadImage(null, imageView, url, 0);
    }

    public static void loadImage(ImageView imageView, String url, int resDefault) {
        loadImage(null, imageView, url, resDefault);
    }

    public static void loadImage(Context context, ImageView imageView, String url) {
        loadImage(context, imageView, url, 0);
    }

    public static void loadImage(Context context, ImageView imageView, String url, int resDefault) {
        if (imageView == null) {
            return;
        }
        final int d = resDefault > 0 ? resDefault : R.mipmap.selection_holder;
        final Context ctx = (context != null ? context : imageView.getContext());//.getApplicationContext();//配合Activity的pause和resume，或许可以不用拥APP的context
        Glide.with(ctx)
                .load(url)
                //.placeholder(d)
                .error(d)
                //.fitCenter().dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    /**
     * 获取缓存到bitmap
     */
    public static void getCacheTo(Context appContext, String url, int width, int height, OnGetCacheCallback callback) {
        //todo 获取图片还需要改进
        new CacheTask(url, width, height, callback).execute(appContext);
    }

    public static void clearAllCache(Context appContext) {
        new ClearTask().execute(appContext.getApplicationContext());
    }

    public interface OnGetCacheCallback {
        public void onResult(Bitmap bitmap);
    }

    private static class CacheTask extends AsyncTask<Context, Void, Bitmap> {
        private String url;
        private int width, height;
        private OnGetCacheCallback callback;

        private CacheTask(String url, int width, int height, OnGetCacheCallback callback) {
            this.url = url;
            this.width = width <= 0 ? Target.SIZE_ORIGINAL : width;
            this.height = height <= 0 ? Target.SIZE_ORIGINAL : height;
            this.callback = callback;
        }

        @Override
        protected Bitmap doInBackground(Context... contexts) {
            Bitmap bitmap = null;
            try {
                final Context appContext = contexts[0];
                final File file = Glide.with(appContext.getApplicationContext())
                        .load(url)
                        .downloadOnly(width, height)
                        .get();
                bitmap = BitmapFactory.decodeFileDescriptor(new FileInputStream(file).getFD());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (callback != null) callback.onResult(bitmap);
        }
    }

    private static class ClearTask extends AsyncTask<Context, Void, Context> {
        @Override
        protected Context doInBackground(Context... contexts) {
            Context appContext = null;
            try {
                appContext = contexts[0];
                Glide.get(appContext).clearDiskCache();
                Glide.get(appContext).clearMemory();
                //FileClear.delete(appContext.getCacheDir());//这个是缓存目录的，原本不属于image的，但刚好这边有个异步的，放这里的话，效果更理想
            } catch (Exception e) {
                e.printStackTrace();
            }
            return appContext;
        }

        @Override
        protected void onPostExecute(Context context) {
            super.onPostExecute(context);
            ToastUtil.showToast(context, "清除缓存成功");
        }
    }
}
